
message = {};

message['textDocument/completion'] = function(json,lsp)
    if(lsp and lsp.codeCompleteAt)then
        local uri = json.params.textDocument.uri;
        local path = UriToPath(uri);
        local line,col = json.params.position.line+1,json.params.position.character+1;
        lsp:codeCompleteAt(json,path,line,col);
    elseif(lsp)then
        lsp:UndefMessage(json);
    end
end

message['initialize'] = function(json,lsp)
    if(lsp and lsp.init)then
        local ret = lsp.init(lsp,json);
        local out = RPC_tmefile();
        out:write('{');
        if(json.id)then
            out:write('"id":',json.id,',');
        end
        out:write('"jsonrpc":"2.0","result":');
        out:write(toJson(ret));
        out:write('}');
        RPC_writeOut(out);

        --if(ret.capabilities and ret.capabilities.textDocumentSync)then
            --设置获得代码的函数
            function lsp:GetCode(json)
                if(self.codes)then
                    return self.codes[json.params.textDocument.uri];
                end
            end

            lsp['textDocument/didChange'] = function(self,json)
                local path = UriToPath(json.params.textDocument.uri);
                if(path)then
                    local code = json.params.contentChanges[1].text;
                    if(self.codes==nil)then self.codes = {}; end;
                    self.codes[json.params.textDocument.uri] = code;
                    if(self.didChange)then
                        self:didChange(json,path,code);
                    end
                end
            end
        --end
        --RPC_writeOut([[{"id":0,"jsonrpc":"2.0","result":{"capabilities":{"textDocumentSync":1,"completionProvider":{"triggerCharacters":[".",">",":"],"resolveProvider":false}}}}]]);
    elseif(lsp)then
        lsp:UndefMessage(json);
    end
end

function Message_proc(json,lsp)
    local proc = false;
    if(lsp.Message)then
        proc = lsp:Message(json);
    end
    if(proc==false  or proc==nil)then
        if(message[json.method])then
            message[json.method](json,lsp);
        elseif(lsp[json.method])then
            lsp[json.method](lsp,json);
        elseif(lsp.UndefMessage)then
            lsp:UndefMessage(json);
        end
    end
end


